package com.atguigu.bigdata.streaming

import java.io.{BufferedReader, InputStream, InputStreamReader}
import java.net.Socket

import org.apache.spark.SparkConf
import org.apache.spark.storage.StorageLevel
import org.apache.spark.streaming.dstream.ReceiverInputDStream
import org.apache.spark.streaming.kafka.KafkaUtils
import org.apache.spark.streaming.receiver.Receiver
import org.apache.spark.streaming.{Seconds, StreamingContext}

//自定义采集器
object SparkStreaming04_KafkaSource {
//  使用SparkStreaming 完成WordCount
  def main(args: Array[String]): Unit = {
//    spark配置对象
     val sparkConf = new SparkConf().setMaster("local[*]").setAppName("SparkStreaming01_WordCount")
//   实时数据分析环境对象
//   采集周期，以指定的时间为周期采集实时数据
     val streamingContext = new StreamingContext(sparkConf,Seconds(3))
//    从kafka中采集数据
    val kafkaDStream:ReceiverInputDStream[(String,String)] = KafkaUtils.createStream(
      streamingContext,
      "10.21.13.181",
      //      以消费者组的概念消费数据
      "first",
      //      Map("first"->3)== first是topic
      Map("first" -> 3)
    )


//   从指定文件夹中读取采集数据
//     val receiverDStream = streamingContext.receiverStream(new MyReceiver("hadoop102",9999))
//   将采集的数据进行分解（扁平化）
     val wordDStream = kafkaDStream.flatMap(t=>t._2.split(" "))
//   将数据进行结构的转换方便统计
     val mapDStream = wordDStream.map((_,1))
//
     val wordToSumDStream = mapDStream.reduceByKey(_+_)

    wordToSumDStream.print()
//    不能停止采集
//    streamingContext.stop()
//   启动采集器
    streamingContext.start()
//    Driver等待采集器的执行
    streamingContext.awaitTermination()
  }
}

